//
//  GSStrategyViewController.m
//  GameSky
//
//  Created by lanou on 16/1/10.
//  Copyright © 2016年 NiRongfei. All rights reserved.
//

#import "GSStrategyViewController.h"
#import "GSStrategyFocusCollectionViewController.h"
#import "GSStrategyDetailViewController.h"
#import "GSStrategyAllTableViewController.h"

#define kMargin 10

@interface GSStrategyViewController () <UIScrollViewDelegate,GSStrategyFocusDelegate,GSStrategyAllDelegate>

@property (nonatomic, strong) GSStrategyFocusCollectionViewController *focusCVC;// 关注界面控制器
@property (nonatomic, strong) GSStrategyAllTableViewController *allTVC;// 全部界面控制器

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIScrollView *titleScrollView;// 标题ScrollView

@property (nonatomic, strong) GSTitleLabel *leftLabel;// 关注标题
@property (nonatomic, strong) GSTitleLabel *rightLabel;// 全部标题

@property (nonatomic, assign) BOOL loaded;

@end

@implementation GSStrategyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.loaded = YES;
    
    [self addSubviews];
    // 设置自定义导航栏;
    [self setupNavigationBar];
}

- (void)addSubviews
{
    // 1.创建ScrollView;
    self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.scrollView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
    self.scrollView.contentSize = CGSizeMake(kScreenWidth * 2, kScreenHeight);
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    [self.view insertSubview:self.scrollView belowSubview:self.navBar];
    
    // 2.根据FlowLayout创建关注界面控制器;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = kMargin;
    layout.minimumInteritemSpacing = kMargin;
    layout.itemSize = CGSizeMake((kScreenWidth - kMargin * 3) / 2, 130);
    layout.headerReferenceSize = CGSizeMake(0, 44);
    layout.sectionInset = UIEdgeInsetsMake(kMargin, kMargin, kMargin, kMargin);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.focusCVC = [[GSStrategyFocusCollectionViewController alloc] initWithCollectionViewLayout:layout];
    self.focusCVC.delegate = self;
    self.focusCVC.collectionView.frame = CGRectMake(0, 20, kScreenWidth, kScreenHeight - 20);
    self.focusCVC.collectionView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight + 50);
    [self.scrollView addSubview:self.focusCVC.collectionView];

    // 创建全部界面控制器;
    self.allTVC = [[GSStrategyAllTableViewController alloc] initWithStyle:(UITableViewStylePlain)];
    self.allTVC.delegate = self;
    self.allTVC.tableView.frame = CGRectMake(kScreenWidth, 20, kScreenWidth, kScreenHeight - 20);
    [self.scrollView addSubview:self.allTVC.tableView];
}

// 设置自定义导航栏;
- (void)setupNavigationBar
{
    // 右边按钮
    [self.navBar.rightButton setImage:[UIImage imageNamed:@"common_Icon_Search_20x20_UIMode_Day"] forState:(UIControlStateNormal)];
    
    // "关注"标题
    self.leftLabel = [[GSTitleLabel alloc] initWithFrame:CGRectMake(self.navBar.titleScrollView.width / 2 - 45, 7, 40, 30)];
    UITapGestureRecognizer *leftTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftTapAction:)];
    [self.leftLabel addGestureRecognizer:leftTap];
    self.leftLabel.text = @"关注";
    self.leftLabel.scale = 1;
    [self.navBar.titleScrollView addSubview:self.leftLabel];
    
    // "全部"标题
    self.rightLabel = [[GSTitleLabel alloc] initWithFrame:CGRectMake(self.navBar.titleScrollView.width / 2 + 5, 7, 40, 30)];
    UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rightTapAction:)];
    [self.rightLabel addGestureRecognizer:rightTap];
    self.rightLabel.text = @"全部";
    [self.navBar.titleScrollView addSubview:self.rightLabel];
}
// "关注"标题添加的手势方法;
- (void)leftTapAction:(UITapGestureRecognizer *)tap
{
    [UIView animateWithDuration:0.5 animations:^{
        // 改变标题缩放比例;
        self.leftLabel.scale = 1;
        self.rightLabel.scale = 0;
        // 改变偏移量;
        [self.scrollView setContentOffset:CGPointMake(0, 0)];
    }];
}
// "全部"标题添加的手势方法;
- (void)rightTapAction:(UITapGestureRecognizer *)tap
{
    [UIView animateWithDuration:0.5 animations:^{
        self.leftLabel.scale = 0;
        self.rightLabel.scale = 1;
        [self.scrollView setContentOffset:CGPointMake(kScreenWidth, 0)];
    }];
}

/**
 *  根据ScrollView偏移量加载数据
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 根据ScrollView偏移量加载相应界面;
    if (scrollView.contentOffset.x >= kScreenWidth / 2 && self.loaded) {
        [self.allTVC loadDataWithPageIndex:1];
        self.loaded = NO;
    }
    
    if (scrollView.contentOffset.x > 0 && scrollView.contentOffset.x < kScreenWidth) {
        // 获得当前偏移量比例;
        CGFloat currentScale = scrollView.contentOffset.x / kScreenWidth;
        // 左边缩小比例
        CGFloat leftScale = 1 - currentScale;
        self.leftLabel.scale = leftScale;
        // 右边放大比例饿
        CGFloat rightScale = currentScale;
        self.rightLabel.scale = rightScale;
    }
}

// 关注界面传值代理方法;
- (void)setStrategyDetailViewWithModel:(GSStrategyMainModel *)model
{
    // 隐藏标签栏;
    self.tabBarController.tabBar.hidden = YES;
    
    GSStrategyDetailViewController *detailVC = [[GSStrategyDetailViewController alloc] init];
    detailVC.model = model;
    [self.navigationController pushViewController:detailVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
